<?php
class ArchiveController extends Zend_Controller_Action
{
	public function init()
	{
		IndexController::initAdmin($this);
		
		$this->_arrNoModify			= array('sku', 'product_name', '_bucket', 'category',  'shipment_type', 'total_sales', 'average_daily_sales', 'forecasts_day', 'exponential_smoothening', 'moving_average' ,'msl', 'asl', 'in_transit',
			'reorder','t_1', 't_2', 't_3', 't_4', 'tsl', '_bucket');
		
		$this->_arrMarketing		= array('sku', 'product_name', 'category', 'shipment_type','total_sales', 'average_daily_sales', 'forecasts_day',
			't_1', 't_2', 't_3', 't_4' ,'tsl', 'moving_average', 'marketing_check', 'marketing_suggest', 'marketing_reason');
		
		$this->_arrBuying			= array('sku', 'product_name', 'category',  'shipment_type', 'total_sales', 'average_daily_sales', 'forecasts_day',
			't_1', 't_2', 't_3', 't_4', 'tsl', 'moving_average' ,'buying_check', 'buying_suggest', 'buying_reason');
		
		$this->_arrInventory		= array('sku', 'product_name', 'category',  'shipment_type', 'total_sales', 'average_daily_sales', 'forecasts_day',
			'inventory_check', 'final_quantity','marketing_suggest', 'marketing_reason', 'buying_suggest', 
			'buying_reason', 'tsl','moving_average', 'exponential_smoothening' ,'msl', 'asl', 'in_transit',
			'reorder','t_1', 't_2', 't_3', 't_4', '_bucket');
		
		$this->_arrInventoryReadOnly = array('marketing_suggest', 'marketing_reason', 'buying_suggest', 'buying_reason');
		
		$this->_arrAdminstrator		= array('sku', 'product_name', 'category',  'shipment_type', 'total_sales','average_daily_sales', 'forecasts_day',
			'inventory_check',  'final_quantity', 'marketing_check', 'marketing_suggest', 'marketing_reason', 'buying_check', 'buying_suggest', 
			'buying_reason', 'exponential_smoothening', 'moving_average' ,'msl', 'asl', 'in_transit',
			'reorder','t_1', 't_2', 't_3', 't_4', '_bucket');
		
		$this->_arrCheckBox = array('marketing_check', 'inventory_check',  'buying_check');
		
		$this->_arrAlignLeft = array('sku', 'product_name', '_bucket');
		
	    $this->_Tooltips = QTooltip:: search($this->AppUI->country, "forecasts");

		$arrayHeader = array();
		
		foreach ($this->_Tooltips as $item)
		{
			$arrayHeader[$item['column']] = $item['name_show'];
		}	
		
		$this->_arrColumnTitle =  $arrayHeader;
		
		$this->_arrExcludeColumn	= array('_rmse', '_slope', '_intercept', '_alpha', 'id');
		if($this->AppUI->admin_group_id==GROUP_Administrator){
			$this->_arrExcludeColumn	= array('_rmse', '_slope', '_intercept', 'id');
		}
		
		$this->view->_arrExcludeColumn	= $this->_arrExcludeColumn;
		$this->view->_arrNoModify		= $this->_arrNoModify;
		
		$this->view->arrMarketing	= $this->_arrMarketing;
		$this->view->arrBuying		= $this->_arrBuying;
		$this->view->arrInventory	= $this->_arrInventory;
		
		$this->view->arrColumnTitle = $this->_arrColumnTitle;
		$this->view->arrCheckBox	= $this->_arrCheckBox;
		$this->view->arrAdminstrator	= $this->_arrAdminstrator;
		$this->view->arrAlignLeft = $this->_arrAlignLeft;
		
		$staticLink = CConfig::link_image();
		$this->view->staticLink =$staticLink[$this->AppUI->country];
	}
	
	public function indexAction()
	{
		$intPage		= $this->_request->getParam('page', 1);
		$intLimit		= $this->_request->getParam('li', 30);
		$strSku			= $this->_request->getParam('sku', null);
		$strTotalSales	= $this->_request->getParam('total', null);
		$strASL			= $this->_request->getParam('asl', null);
		$strSupplier	= $this->_request->getParam('supplier', null);
		$strType		= $this->_request->getParam('t', 'archive');
		$strTableName	= $this->_request->getParam('n', null);
		$strReorder		= $this->_request->getParam('re', null);
		$strOrderField	= $this->_request->getParam('orf', 'reorder');
		$strOrderValue	= $this->_request->getParam('orv', 'default');
		$arrStatusId	= $this->_request->getParam('st', array());
		
		$arrParams	= array
		(
			'sku'		=> trim($strSku),
			'total'		=> trim($strTotalSales),	
			'asl'		=> trim($strASL),
			'supplier'	=> trim($strSupplier),
			'reorder'	=> trim($strReorder),
			'orf'		=> trim($strOrderField),
			'orv'		=> trim($strOrderValue)
		);
		
		$objCategory	= new CCategory($this->AppUI->country);
		$arrData		= $objCategory->get();
		$arrCategoryData= isset($arrData['category']) ? $arrData['category'] : null;
		$arrCategory	= array();
		
		if($this->AppUI->admin_group_id==GROUP_Buying)
		{
			$arrCategory	= $this->AppUI->admin_category ? explode(';', $this->AppUI->admin_category) : null;
			$arrCategoryData= $arrCategory;
			if(!$arrCategory){
				$arrCategory	= array('xyz');
			}	
		}	
		else
		{ 
			foreach($arrCategoryData as $data){
				$arrCategory[]	= trim($data['name']);
			}
		} 
		
		$arrCategoryData= $arrCategory;
		$arrCategory	= $arrStatusId ? $arrStatusId : $arrCategory;
		
		$obj		= new QArchive($this->AppUI->country);
		$intTotal	= $obj->total($arrCategory, $arrParams);
		
		if($intTotal)
		{
			$intOffset	= ($intPage-1)*$intLimit;
			$arrResult	= $obj->get($intOffset, $intLimit, $arrCategory, $arrParams, null);
			
			$arrTitle	= array();
			foreach($arrResult as $data)
			{
				foreach($data as $key=>$value){
					$arrTitle[]	= $key;
				}
				break;
			}
			
			$intTotalPage = ceil($intTotal/$intLimit);
			$this->view->intTotalPage = $intTotalPage;
			if ($intTotalPage>1)
			{
				$strQuery = null;
				if ($intLimit!=100){ $strQuery.= '&li='.$intLimit; }
				if ($strReorder){ $strQuery.= '&re='.$strReorder; }
				if($arrStatusId)
				{	
					foreach($arrStatusId as $status){
						$strQuery	.= '&st[]='.urlencode($status);
					}
				}
				if ($strSku){ $strQuery.= '&sku='.$strSku; }
				if ($strTotalSales){ $strQuery.= '&total='.$strTotalSales; }
				if ($strOrderField){ $strQuery.= '&orf='.$strOrderField; }
				if ($strOrderValue){ $strQuery.= '&orv='.$strOrderValue; }
				if ($strTableName){ $strQuery.= '&n='.$strTableName; }
				$objPaginator = new Common_Paginator($intTotalPage, $intPage);
				$objPaginator->setUrl(HOST_ADMIN.'/archive');
				$objPaginator->setQuery($strQuery);
				$this->view->strPaginator = $objPaginator->showAdmin();
			}
			
			$this->view->arrResult	= $arrResult;
			$this->view->arrTitle	= $arrTitle;
		}
		$this->view->strOrderValue		= $strOrderValue;
		$this->view->strOrderField		= $strOrderField;
		$this->view->arrStatusId		= $arrStatusId;
		$this->view->arrCategoryData	= $arrCategoryData;
		$this->view->strReorder			= $strReorder;
		$this->view->strSku				= $strSku;
		$this->view->strTotalSales		= $strTotalSales;
		$this->view->strASL				= $strASL;
		$this->view->strSupplier		= $strSupplier;
		$this->view->intTotal			= $intTotal;
		$this->view->intPage			= $intPage;
		$this->view->intItemOnPerPage	= $intLimit;
		$this->view->strType			= $strType;
		$this->view->jsSelectMulti		= 1;
		
		#Column
		$arrColumn	= array();
		if(!$strTableName)
		{
			$objColumn	= new QColumn($this->AppUI->country);
			$arrData	= $objColumn->get(0);
			
			if($arrData)
			{
				foreach($arrData as $data){
					$arrColumn[]	= trim($data['icolumn']);
				}
			}
		}
		else
		{
			$objArchiveLogs	= new QArchiveLog($this->AppUI->country);
			$arrArchiveLog	= $objArchiveLogs->get_by_id($strTableName);
			$this->view->arrArchiveLog	= $arrArchiveLog;
		}
		$this->view->arrColumn			= $arrColumn;
		
		$this->view->arrSupplier = QSupplier::getAll($this->AppUI->country, 'suppliers');
		$this->view->arrTooltip = $this->_Tooltips;
		#Column
	}

	public function updateFieldAction()
	{
		if($this->_request->isPost())
		{
			$intId			= $this->_request->getParam('i', null);
			$strField		= $this->_request->getParam('f', null);
			$mixValue		= $this->_request->getParam('v', null);
			if($intId && $strField && $mixValue)
			{
				$obj		= new QArchive($this->AppUI->country);
				$arrParams	= array($strField=>$mixValue);
				$obj->update($arrParams, $intId);
				echo json_encode(array('result'=>1));
			}
		}
		exit;
	}
	
	public function exportAction()
	{
		ini_set('memory_limit', '-1');	
		$objCategory	= new CCategory($this->AppUI->country);
		$arrData		= $objCategory->get();
		$arrCategoryData= isset($arrData['category']) ? $arrData['category'] : null;
		
		$strTableName	= $this->_request->getParam('n', null);
		$obj			= new QArchive($this->AppUI->country, $strTableName);
		
		$arrCategory =array();
		if($this->AppUI->admin_group_id==GROUP_Buying)
		{
			$arrCategory	= $this->AppUI->admin_category ? explode(';', $this->AppUI->admin_category) : null;
			$arrCategoryData= $arrCategory;
			if(!$arrCategory){
				$arrCategory	= array('xyz');
			}	
		}	
		else
		{
			foreach($arrCategoryData as $data){
				$arrCategory[]	= trim($data['name']);
			}
		}
		
		$arrResult	= $obj->get(0, 'all', $arrCategory, null, 'export');
	
		
		if($arrResult)
		{
			$arrTitle	= array();
			foreach($arrResult as $data)
			{
				foreach($data as $key=>$value){
					if( !in_array($key, $this->_arrExcludeColumn) ){
						$arrTitle[]	= ucfirst(trim(str_replace('_', ' ', $key)));
					}
				}
				break;
			}
			
			$strFname	= $strTableName ? $strTableName : $this->AppUI->country.'_forecast_'.date('YmdHis');
			$strFname	= $strFname.'.csv';
			$strCSV		= null;
			$strCSV		= $strCSV. implode(';', $arrTitle)."\r\n";
			foreach($arrResult as $data)
			{
				$arrRecord		= array();
				foreach($data as $key=>$value)
				{
					if( !in_array($key, $this->_arrExcludeColumn) ){
						$arrRecord[]	= $value;
					}
				}
				$strCSV	= $strCSV. implode(';', $arrRecord)."\r\n";
			}
			$strCSV = chr(255).chr(254).mb_convert_encoding($strCSV, "UTF-16LE", "UTF-8");	
			header("Content-type: application/ms-excel");
			header("Content-disposition: attachment; filename=".$strFname."; size=".strlen($strCSV));
			header('Cache-Control: max-age=0'); 
			echo $strCSV;
		}
		exit;
	}
}
?>
